const vm = new Vue({
    el: '#main',
    data: {
        view: {
            end: false,
        },
        data: {
            going: [],
            end: [],
        },
        pageNum: 1,
    },
    methods: {
        toggleEnd(b) {
            this.view.end = b;
        },
        cancelIndent,
        earlyLeave,
        checkIndentDetail,
        contactTheLandlord,
        delIndent,
        nowPay,
        goPage,
    },
});

function main() {
    fxj.fixLayout();
    reqOrderList(1);
    initPullDownLoad();
}

function apiready() {
    main();
    setCustomRefreshHeaderInfo(() => {
        reqOrderList(1);
    });
}

async function reqOrderList(pageNum = vm.pageNum) {
    const res = await ajax({
        url: 'home/order/getOrderList',
        data: {
            pageNum,
            role: 2,
        },
    });
    const {underWay, over} = res.data;
    if (underWay.length !== 0) {
        fixImg(underWay);
    }
    if (over.length !== 0) {
        fixImg(over);
    }
    if (pageNum === 1) {
        vm.data.going = underWay;
        vm.data.end = over;
    }
    else {
        vm.data.going.push(...underWay);
        vm.data.end.push(...over);
    }
}

function cancelIndent(indentId) {
    fxj.dialog.alert({
        title: '提示',
        msg: '您确定要取消订单吗',
        btnLeft: '我要取消订单',
        btnRight: '不小心点错了',
        leftCallback: async () => {
            const res = await ajax({
                url: 'home/order/cancel',
                data: {
                    orderId: indentId,
                },
            });
            if (res.err === 0) {
                toast('取消订单成功');
                setTimeout(() => {
                    reqOrderList(1);
                }, 1000);
            }
            else {
                toast(res.msg);
            }
        },
    });
}

async function delIndent(indentId) {
    const close = fxj.dialog.loading({noBg: true});
    const res = await ajax({
        url: 'home/order/deleteOrder',
        data: {
            orderId: indentId,
            role: 2,
        },
    });
    close();
    if (res.err === 0) {
        mui.toast('删除成功');
        reqOrderList(1);
    }
}

function contactTheLandlord(indent) {
    goPage('chat.html', {
        data: {
            landlordId: indent.landlord_id,
            type: '联系房东',
        },
    });
}

function checkIndentDetail(id) {
    goPage('indent_detail.html', {
        data: {
            indentId: id,
            role: 'fk',
        },
    });
}

async function earlyLeave(indentId) {
    const res = await ajax({
        url: 'home/order/cancel',
        data: {
            orderId: indentId,
        },
    });
    reqOrderList(1);
}

function nowPay(indent) {
    goPage('pay_deposit.html', {
        data: {
            id: indent.oid,
        },
    });
}

function fixImg(list) {
    forEach(list, (l) => {
        l.img = fxj.fixImgUrl(l.img);
    });
}

// 下拉加载更多
function initPullDownLoad() {
    const cb = (ok) => {
        if (!ok) return;
        vm.pageNum += 1;
        reqOrderList(vm.pageNum);
    };
    isToBottom(10, cb);
}

